package com.dc.base.utils;

import android.content.Context;
import android.graphics.Bitmap;

import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;

/**
 * 微信工具类
 *
 * @author 冰柠檬dc
 */
public class WeChatUtil {

    private IWXAPI iwxapi;

    private WeChatUtil() {
    }

    private static class Holder {
        private static final WeChatUtil INSTANCE = new WeChatUtil();
    }

    public static WeChatUtil getInstance() {
        return Holder.INSTANCE;
    }

    public void register(Context context, String appId) {
        iwxapi = WXAPIFactory.createWXAPI(context, appId, true);
        iwxapi.registerApp(appId);
    }

    public void loginWithWeiXin(Context context) {
        if (iwxapi != null) {
            if (!iwxapi.isWXAppInstalled()) {
                // 提醒用户没有安装微信
                return;
            }
            SendAuth.Req req = new SendAuth.Req();
            req.scope = "snsapi_userinfo";
            req.state = "wechat_sdk";
            iwxapi.sendReq(req);
        }
    }

    public void shareWeb(Context context, String webUrl, String title, String content, int scene, Bitmap bitmap) {
        // 检查手机或者模拟器是否安装了微信
        if (!iwxapi.isWXAppInstalled()) {
            return;
        }
        if (iwxapi != null) {
            // 初始化一个WXWebpageObject对象
            WXWebpageObject webpageObject = new WXWebpageObject();
            // 填写网页的url
            webpageObject.webpageUrl = webUrl;
            // 用WXWebpageObject对象初始化一个WXMediaMessage对象
            WXMediaMessage msg = new WXMediaMessage(webpageObject);
            // 填写网页标题、描述、位图
            msg.title = title;
            msg.description = content;
            // 如果没有位图，可以传null，会显示默认的图片
            msg.setThumbImage(bitmap);
            // 构造一个Req
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            // transaction用于唯一标识一个请求（可自定义）
            req.transaction = "webpage";
            // 上文的WXMediaMessage对象
            req.message = msg;
            // SendMessageToWX.Req.WXSceneSession是分享到好友会话
            // SendMessageToWX.Req.WXSceneTimeline是分享到朋友圈
            req.scene = scene;
            // 向微信发送请求
            iwxapi.sendReq(req);
        }
    }

    public void shareBitmapImage(Context context, Bitmap bitmap, String title, String content, int scene) {
        if (iwxapi != null) {
            // 检查手机或者模拟器是否安装了微信
            if (!iwxapi.isWXAppInstalled()) {
                return;
            }
            WXImageObject imgObj = new WXImageObject(bitmap);
            WXMediaMessage msg = new WXMediaMessage();
            msg.mediaObject = imgObj;
            Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, 120, 120 * bitmap.getHeight() / bitmap.getWidth(), true);
            bitmap.recycle();
            //设置缩略图
            msg.thumbData = bmpToByteArray(thumbBitmap);
            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = "img";
            req.message = msg;
            req.scene = scene;
            iwxapi.sendReq(req);
        }
    }

    private byte[] bmpToByteArray(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
}
